package com.hok.misc.console;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class ConsoleTest {
	public static void main(String[] args) {
//		System.out.println(readString5("input ip:"));
//		System.out.println(readString6("please input:"));
		System.out.println(readString("please input:\n"));
	}

	public static String console1() {
		String msg = null;
		msg = JOptionPane.showInputDialog("input ");
		return msg;
	}

	public static String readString5(String prompt) {
		Scanner scanner = new Scanner(System.in);
		System.out.print(prompt);
		return scanner.nextLine();
	}

    
    /** 
     * 使用控制台类（Console）从控制台中读取字符串<br/> 
     * 适用于JDK 1.6或以后的版本 
     * @param prompt 提示信息 
     * @return 输入的字符串 
     */ 
	public static String readString6(String prompt) { 
        Console console = System.console(); 
        if (console == null) { 
            throw new IllegalStateException("不能使用控制台"); 
        } 
        return console.readLine(prompt); 
    } 
	
	   /** 
     * 使用系统的输入流，从控制台中读取数据<br/> 
     * 用于所用的JDK版本 
     * @param prompt 提示信息 
     * @return 输入的字符串 
     */ 
    private static String readString(String prompt) { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        String str = null; 
        try { 
            System.out.print(prompt); 
            str = br.readLine(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
} 

